﻿using System;
using System.Collections.Generic;
using System.Data;


namespace IncubatorServices
{
    public class Company
    {
        public int id;
        public string name;
        public int money;
        public string description;
        public List<int> stocks_owned;
        public List<int> stocks_owned_by;

        public List<int> quiz_bought;

        public Company()
        {
        }

        public void RegisterNewCompany(string name_, int money_, string description_)
        {//creates new company, associates it with given user.
            id = createNew();
            name = name_;
            money = money_;
            description = description_;

            storeData();

        }

        public void storeData()
        {
            sqlManager sql = new sqlManager();
            string query = "UPDATE company SET name = '" + name + "', " +
                "money = '" + money + "', " +
                "description = '" + description + "'" +
                "WHERE id = '" + id + "'";

            sql.NonQuery(query);
        }

        public void LoadData(int id_)
        {
            sqlManager sql = new sqlManager();
            string query = "SELECT name, money, description FROM company WHERE id = '" + id_ + "'";

            DataSet ds = sql.GetDataSet(query);
            id = id_;
            this.name = (string)(ds.Tables[0].Rows[0]["name"]);
            this.description = (string)ds.Tables[0].Rows[0]["description"];
            this.money = Int32.Parse(sqlManager.GetData(ds.Tables[0].Rows[0]["money"]));

            query = "SELECT quiz_id, completed_questions FROM quiz_status WHERE company_id = '" + id + "'";
            quiz_bought = new List<int>();
            ds = sql.GetDataSet(query);
            foreach (DataRow r in ds.Tables[0].Rows)
            {
                quiz_bought.Add((int)r["quiz_id"]);

            }

        }
        private int createNew()
        {
            sqlManager sql = new sqlManager();
            string query = "INSERT INTO company (name, money, description) VALUES('',0,'')";
            sql.NonQuery(query);
            return Int32.Parse(sql.GetScalar("SELECT MAX(id) as max FROM company"));

        }
        public User GetFounder()
        {
            sqlManager sql = new sqlManager();
            string query = "SELECT id FROM users WHERE company_id = '" + id + "'";
            int user_id = Int32.Parse(sql.GetScalar(query));
            User u = new User();
            u.LoadData(user_id);
            return u;
            
        }
    }
}